package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * projectName: wokepase1
 *
 * @author: 强力
 * time: 2021/9/15 14:42
 * description:
 */
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
private DtsCategoryMapper categoryMapper;
    @Override
    public IPage<DtsCategory> findByPage(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        Page<DtsCategory> categoryPage=new Page<>(page,limit);

        QueryWrapper<DtsCategory> queryWrapper=new QueryWrapper<>();
        if (id != null){
            queryWrapper.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (order.equalsIgnoreCase("asc")){
            queryWrapper.orderByAsc(sort);
        }else {
            queryWrapper.orderByDesc(sort);
        }
        IPage<DtsCategory> list = categoryMapper.selectPage(categoryPage, queryWrapper);
        return list;
    }

    @Override
    public List<DtsCategory> findcategory() {
        List<DtsCategory> list = categoryMapper.selectList(null);

        return list;
    }

    @Override
    public DtsCategory insertCategory(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        categoryMapper.insert(category);
        return category;


    }

    @Override
    public DtsCategory updateCatgory(DtsCategory category) {
        QueryWrapper<DtsCategory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",category.getId());
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        categoryMapper.update(category,queryWrapper);
        return category;
    }

    @Override
    public DtsCategory deleteCategory(DtsCategory category) {
        QueryWrapper<DtsCategory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",category.getId());
        categoryMapper.delete(queryWrapper);
        return category;
    }

    @Override
    public List<DtsCategory> findcategoryList() {
        List<DtsCategory> categories = categoryMapper.selectList(null);
        return categories;
    }
}
